{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4ea0b2be",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import warnings\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "%matplotlib inline\n",
    "warnings.filterwarnings('ignore')\n",
    "plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签\n",
    "plt.rcParams['axes.unicode_minus']=False #用来正常显示负号\n",
    "import missingno"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f2710236",
   "metadata": {},
   "outputs": [],
   "source": [
    "data3=pd.read_csv('farming.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "483ebb0b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from sklearn.ensemble import RandomForestRegressor\n",
    "from sklearn.metrics import r2_score\n",
    "\n",
    "sell = [\"农产品名称映射值\", \"区域\", \"颜色\", \"单位\",\"数据入库年份\",\"数据入库月份\"]\n",
    "\n",
    "# 准备数据\n",
    "X = data3[sell][:950000]  # 特征\n",
    "y2 = data3['平均交易价格'][:950000]  # 目标变量2\n",
    "groud_truth2 = data3['平均交易价格'][950001:]\n",
    "\n",
    "# 存放不同参数取值，以及对应的精度\n",
    "results2 = []\n",
    "\n",
    "# 最小叶子结点的参数取值\n",
    "sample_leaf_options = list(range(1, 500, 300))\n",
    "# 决策树个数参数取值\n",
    "n_estimators_options = list(range(1, 1000, 500))\n",
    "\n",
    "for leaf_size in sample_leaf_options:\n",
    "    for n_estimators_size in n_estimators_options:\n",
    "        alg = RandomForestRegressor(min_samples_leaf=leaf_size, n_estimators=n_estimators_size, random_state=50)\n",
    "        alg.fit(X, y2)\n",
    "        predict2 = alg.predict(data3[sell][950001:])\n",
    "        r2 = r2_score(groud_truth2, predict2)\n",
    "        results2.append((leaf_size, n_estimators_size, r2))\n",
    "        print(f\"Leaf size: {leaf_size}, Estimators size: {n_estimators_size}, R2: {r2}\")\n",
    "\n",
    "# 打印精度最大的那一个三元组\n",
    "print(\"Best parameters for 平均交易价格:\")\n",
    "print(max(results2, key=lambda x: x[2]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "8d893704",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (2245276836.py, line 7)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  Cell \u001b[1;32mIn[7], line 7\u001b[1;36m\u001b[0m\n\u001b[1;33m    from sklearn.metrics import mean squared error, r2_score\u001b[0m\n\u001b[1;37m                                     ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.ensemble import RandomForestRegressor\n",
    "from sklearn.preprocessing import OneHotEncoder\n",
    "from sklearn.compose import ColumnTransformer\n",
    "from sklearn.pipeline import Pipeline\n",
    "from sklearn.metrics import mean squared error, r2_score\n",
    "from sklearn.metrics import r2_score\n",
    "#假设'df 是你的 DataFrame#df=pd.read csv('your data file.csv')#如果你是从文件加载数据\n",
    "#定义分类特征和数值特征的列名\n",
    "categorical_features =[ '农产品市场所在省份','农产品类别','颜色','单位']\n",
    "numerical_features=['平均交易价格','最高交易价格']\n",
    "#创建一个 ColumpTransformer 来处理分类特征\n",
    "preprocessor =ColumnTransformer(\n",
    "    transformers=[\n",
    "        ('cat',0neHotEncoder(sparse=False),categorical_features),\n",
    "        ('num','passthrough',numerical_features)\n",
    "#应用预处理器处理数据\n",
    "pipeline =Pipeline(steps=[('preprocessor', preprocessor)])\n",
    "processed_data=pipeline.fit_transform(df4)\n",
    "#分制特征和目标变量，假设目标变量是，平均交易价格’\n",
    "Y=processed_data[:，2] #因为目标变量，平均交易价’是倒数第二列\n",
    "X=processed_datal:,:2]\n",
    "#拆分数据集\n",
    "X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.3, random_state=536)\n",
    "rf = RandomForestRegressor(n_estimators=321,max_depth=25)#建立模型\n",
    "rf.fit(X_train, Y_train)\n",
    "pred = rf.predict(X_test)\n",
    "score_f= rf.score(X_test,Y_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "4f7117d2",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "A given column is not a column of the dataframe",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "File \u001b[1;32mD:\\python\\Anaconda\\lib\\site-packages\\pandas\\core\\indexes\\base.py:3629\u001b[0m, in \u001b[0;36mIndex.get_loc\u001b[1;34m(self, key, method, tolerance)\u001b[0m\n\u001b[0;32m   3628\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m-> 3629\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_engine\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget_loc\u001b[49m\u001b[43m(\u001b[49m\u001b[43mcasted_key\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m   3630\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mKeyError\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m err:\n",
      "File \u001b[1;32mD:\\python\\Anaconda\\lib\\site-packages\\pandas\\_libs\\index.pyx:136\u001b[0m, in \u001b[0;36mpandas._libs.index.IndexEngine.get_loc\u001b[1;34m()\u001b[0m\n",
      "File \u001b[1;32mD:\\python\\Anaconda\\lib\\site-packages\\pandas\\_libs\\index.pyx:163\u001b[0m, in \u001b[0;36mpandas._libs.index.IndexEngine.get_loc\u001b[1;34m()\u001b[0m\n",
      "File \u001b[1;32mpandas\\_libs\\hashtable_class_helper.pxi:5198\u001b[0m, in \u001b[0;36mpandas._libs.hashtable.PyObjectHashTable.get_item\u001b[1;34m()\u001b[0m\n",
      "File \u001b[1;32mpandas\\_libs\\hashtable_class_helper.pxi:5206\u001b[0m, in \u001b[0;36mpandas._libs.hashtable.PyObjectHashTable.get_item\u001b[1;34m()\u001b[0m\n",
      "\u001b[1;31mKeyError\u001b[0m: '农产品市场所在省份'",
      "\nThe above exception was the direct cause of the following exception:\n",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "File \u001b[1;32mD:\\python\\Anaconda\\lib\\site-packages\\sklearn\\utils\\__init__.py:433\u001b[0m, in \u001b[0;36m_get_column_indices\u001b[1;34m(X, key)\u001b[0m\n\u001b[0;32m    432\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m col \u001b[38;5;129;01min\u001b[39;00m columns:\n\u001b[1;32m--> 433\u001b[0m     col_idx \u001b[38;5;241m=\u001b[39m \u001b[43mall_columns\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget_loc\u001b[49m\u001b[43m(\u001b[49m\u001b[43mcol\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    434\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(col_idx, numbers\u001b[38;5;241m.\u001b[39mIntegral):\n",
      "File \u001b[1;32mD:\\python\\Anaconda\\lib\\site-packages\\pandas\\core\\indexes\\base.py:3631\u001b[0m, in \u001b[0;36mIndex.get_loc\u001b[1;34m(self, key, method, tolerance)\u001b[0m\n\u001b[0;32m   3630\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mKeyError\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m err:\n\u001b[1;32m-> 3631\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mKeyError\u001b[39;00m(key) \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01merr\u001b[39;00m\n\u001b[0;32m   3632\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m:\n\u001b[0;32m   3633\u001b[0m     \u001b[38;5;66;03m# If we have a listlike key, _check_indexing_error will raise\u001b[39;00m\n\u001b[0;32m   3634\u001b[0m     \u001b[38;5;66;03m#  InvalidIndexError. Otherwise we fall through and re-raise\u001b[39;00m\n\u001b[0;32m   3635\u001b[0m     \u001b[38;5;66;03m#  the TypeError.\u001b[39;00m\n",
      "\u001b[1;31mKeyError\u001b[0m: '农产品市场所在省份'",
      "\nThe above exception was the direct cause of the following exception:\n",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[12], line 30\u001b[0m\n\u001b[0;32m     28\u001b[0m pipeline \u001b[38;5;241m=\u001b[39m Pipeline(steps\u001b[38;5;241m=\u001b[39m[(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mpreprocessor\u001b[39m\u001b[38;5;124m'\u001b[39m, preprocessor)])\n\u001b[0;32m     29\u001b[0m \u001b[38;5;66;03m# 假设 df 是你的 DataFrame，确保 df 已经正确加载\u001b[39;00m\n\u001b[1;32m---> 30\u001b[0m processed_data \u001b[38;5;241m=\u001b[39m \u001b[43mpipeline\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfit_transform\u001b[49m\u001b[43m(\u001b[49m\u001b[43mdf\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m     32\u001b[0m \u001b[38;5;66;03m# 分割特征和目标变量，假设目标变量是 '平均交易价格'\u001b[39;00m\n\u001b[0;32m     33\u001b[0m X \u001b[38;5;241m=\u001b[39m processed_data[:, \u001b[38;5;241m2\u001b[39m:\u001b[38;5;241m-\u001b[39m\u001b[38;5;241m1\u001b[39m]  \u001b[38;5;66;03m# 特征列\u001b[39;00m\n",
      "File \u001b[1;32mD:\\python\\Anaconda\\lib\\site-packages\\sklearn\\pipeline.py:434\u001b[0m, in \u001b[0;36mPipeline.fit_transform\u001b[1;34m(self, X, y, **fit_params)\u001b[0m\n\u001b[0;32m    432\u001b[0m fit_params_last_step \u001b[38;5;241m=\u001b[39m fit_params_steps[\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39msteps[\u001b[38;5;241m-\u001b[39m\u001b[38;5;241m1\u001b[39m][\u001b[38;5;241m0\u001b[39m]]\n\u001b[0;32m    433\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mhasattr\u001b[39m(last_step, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mfit_transform\u001b[39m\u001b[38;5;124m\"\u001b[39m):\n\u001b[1;32m--> 434\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m last_step\u001b[38;5;241m.\u001b[39mfit_transform(Xt, y, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mfit_params_last_step)\n\u001b[0;32m    435\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m    436\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m last_step\u001b[38;5;241m.\u001b[39mfit(Xt, y, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mfit_params_last_step)\u001b[38;5;241m.\u001b[39mtransform(Xt)\n",
      "File \u001b[1;32mD:\\python\\Anaconda\\lib\\site-packages\\sklearn\\compose\\_column_transformer.py:672\u001b[0m, in \u001b[0;36mColumnTransformer.fit_transform\u001b[1;34m(self, X, y)\u001b[0m\n\u001b[0;32m    670\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_check_n_features(X, reset\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m)\n\u001b[0;32m    671\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_validate_transformers()\n\u001b[1;32m--> 672\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_validate_column_callables\u001b[49m\u001b[43m(\u001b[49m\u001b[43mX\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    673\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_validate_remainder(X)\n\u001b[0;32m    675\u001b[0m result \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_fit_transform(X, y, _fit_transform_one)\n",
      "File \u001b[1;32mD:\\python\\Anaconda\\lib\\site-packages\\sklearn\\compose\\_column_transformer.py:352\u001b[0m, in \u001b[0;36mColumnTransformer._validate_column_callables\u001b[1;34m(self, X)\u001b[0m\n\u001b[0;32m    350\u001b[0m         columns \u001b[38;5;241m=\u001b[39m columns(X)\n\u001b[0;32m    351\u001b[0m     all_columns\u001b[38;5;241m.\u001b[39mappend(columns)\n\u001b[1;32m--> 352\u001b[0m     transformer_to_input_indices[name] \u001b[38;5;241m=\u001b[39m \u001b[43m_get_column_indices\u001b[49m\u001b[43m(\u001b[49m\u001b[43mX\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcolumns\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    354\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_columns \u001b[38;5;241m=\u001b[39m all_columns\n\u001b[0;32m    355\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_transformer_to_input_indices \u001b[38;5;241m=\u001b[39m transformer_to_input_indices\n",
      "File \u001b[1;32mD:\\python\\Anaconda\\lib\\site-packages\\sklearn\\utils\\__init__.py:441\u001b[0m, in \u001b[0;36m_get_column_indices\u001b[1;34m(X, key)\u001b[0m\n\u001b[0;32m    438\u001b[0m             column_indices\u001b[38;5;241m.\u001b[39mappend(col_idx)\n\u001b[0;32m    440\u001b[0m     \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mKeyError\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m e:\n\u001b[1;32m--> 441\u001b[0m         \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mA given column is not a column of the dataframe\u001b[39m\u001b[38;5;124m\"\u001b[39m) \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01me\u001b[39;00m\n\u001b[0;32m    443\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m column_indices\n\u001b[0;32m    444\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n",
      "\u001b[1;31mValueError\u001b[0m: A given column is not a column of the dataframe"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.ensemble import RandomForestRegressor\n",
    "from sklearn.preprocessing import OneHotEncoder\n",
    "from sklearn.compose import ColumnTransformer\n",
    "from sklearn.pipeline import Pipeline\n",
    "from sklearn.metrics import r2_score\n",
    "from sklearn.impute import SimpleImputer\n",
    "#from sklearn.metrics import mean squared_error\n",
    "\n",
    "categorical_features = ['农产品市场所在省份', '农产品类别', '颜色', '单位']\n",
    "numerical_features = ['平均交易价格', '最高交易价格']\n",
    "\n",
    "# 检查这些列名是否都在 DataFrame 中\n",
    "#for column in categorical_features + numerical_features:\n",
    "   # if column not in df.columns:\n",
    "     #   print(f\"Column '{column}' is missing in the DataFrame\")\n",
    "\n",
    "# 创建一个 ColumnTransformer 来处理分类特征\n",
    "preprocessor = ColumnTransformer(\n",
    "    transformers=[\n",
    "        ('cat', OneHotEncoder(sparse=False), categorical_features),\n",
    "        ('num','passthrough', numerical_features)\n",
    "    ]\n",
    ")\n",
    "\n",
    "# 应用预处理器处理数据\n",
    "pipeline = Pipeline(steps=[('preprocessor', preprocessor)])\n",
    "# 假设 df 是你的 DataFrame，确保 df 已经正确加载\n",
    "processed_data = pipeline.fit_transform(df)\n",
    "\n",
    "# 分割特征和目标变量，假设目标变量是 '平均交易价格'\n",
    "X = processed_data[:, 2:-1]  # 特征列\n",
    "Y = processed_data[:, -1]  # 目标变量列\n",
    "\n",
    "# 拆分数据集\n",
    "X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.3, random_state=536)\n",
    "\n",
    "# 建立模型\n",
    "rf = RandomForestRegressor(n_estimators=100, max_depth=10)\n",
    "rf.fit(X_train, Y_train)\n",
    "\n",
    "# 进行预测\n",
    "pred = rf.predict(X_test)\n",
    "\n",
    "# 计算 R² 分数\n",
    "score = r2_score(Y_test, pred)\n",
    "print(f\"R² score: {score}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6392392a",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.ensemble import RandomForestRegressor\n",
    "from sklearn.metrics import r2_score\n",
    "from sklearn.preprocessing import OneHotEncoder\n",
    "from sklearn.compose import ColumnTransformer\n",
    "\n",
    "\n",
    "# 定义分类特征和数值特征的列名\n",
    "categorical_features = [\"农产品名称映射值\", \"区域\", \"颜色\", \"单位\"]\n",
    "numerical_features = [\"数据入库年份\", \"数据入库月份\"]\n",
    "\n",
    "# 创建一个 ColumnTransformer 来处理分类特征\n",
    "preprocessor = ColumnTransformer(\n",
    "    transformers=[\n",
    "        ('cat', OneHotEncoder(sparse=True), categorical_features),\n",
    "        ('num', 'passthrough', numerical_features)\n",
    "    ]\n",
    ")\n",
    "# 应用预处理器处理数据\n",
    "X = preprocessor.fit_transform(data3[categorical_features + numerical_features])\n",
    "y = data3['平均交易价格']\n",
    "\n",
    "# 分割数据集\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=536)\n",
    "\n",
    "# 使用网格搜索来调优模型参数\n",
    "from sklearn.model_selection import GridSearchCV\n",
    "\n",
    "param_grid = {\n",
    "    'n_estimators': [50, 100, 200],\n",
    "    'min_samples_leaf': [1, 2, 4]\n",
    "}\n",
    "\n",
    "rf = RandomForestRegressor(random_state=50)\n",
    "grid_search = GridSearchCV(estimator=rf, param_grid=param_grid, cv=3, scoring='r2')\n",
    "grid_search.fit(X_train, y_train)\n",
    "\n",
    "# 获取最佳参数和模型\n",
    "best_params = grid_search.best_params_\n",
    "best_model = grid_search.best_estimator_\n",
    "\n",
    "# 进行预测\n",
    "y_pred = best_model.predict(X_test)\n",
    "\n",
    "# 计算 R² 分数\n",
    "r2 = r2_score(y_test, y_pred)\n",
    "print(f\"Best parameters: {best_params}\")\n",
    "print(f\"R² score: {r2}\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.19"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
